package vtt.android.lichphatsongbongda;

import org.json.JSONException;
import org.json.JSONObject;

import vtt.android.library.utils.VttGA;
import vtt.android.library.utils.VttLog;
import vtt.android.library.utils.VttUtils;
import vtt.android.lichphatsongbongda.activities.LichPhatSongActivity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;

public class MyPushReceiver extends ParsePushBroadcastReceiver {

    public static final String EXTRA_PARSE_DATA = "com.parse.Data";
    private static final String EXTRA_VERSION_CODE = "version_code";
    private static final String EXTRA_PACKAGE_NAME = "package_name";
    
    private VttLog log = new VttLog("MyPushReceiver", BaseApplication.isDebuggable);

    @Override
    public void onReceive(Context context, Intent intent) {
        log.i("VTT>>> onReceive BaseApplication.mIsMainActivityRunning " + BaseApplication.mIsMainActivityRunning);
        if(!BaseApplication.mIsMainActivityRunning){
            super.onReceive(context, intent);
        } else {
            /* trong truong hop version up thi luon show notify */
            // check version up
            JSONObject pushData;
            try {
                pushData = new JSONObject(intent.getStringExtra(MyPushReceiver.EXTRA_PARSE_DATA));
                if (pushData.has(EXTRA_VERSION_CODE) && checkNewVersion(context, pushData)) {
                    super.onReceive(context, intent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        log.i("VTT>>> onPushOpen " + intent);
        try {
            VttGA.sendEvent(context, "PUSH GCM", "Open Push", null, 0);
            ParseAnalytics.trackAppOpened(intent);
            JSONObject pushData = new JSONObject(intent.getStringExtra(MyPushReceiver.EXTRA_PARSE_DATA));
            if (pushData.has(EXTRA_VERSION_CODE) && checkNewVersion(context, pushData)) {
                if(pushData.has(EXTRA_PACKAGE_NAME) && !pushData.getString(EXTRA_PACKAGE_NAME).isEmpty()){
                    VttUtils.openAppVnMarket(context, pushData.getString(EXTRA_PACKAGE_NAME));
                } else {
                    VttUtils.openAppVnMarket(context, context.getPackageName());
                }
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // other case, open Main screen
        Intent activityIntent = new Intent(context, LichPhatSongActivity.class);
        activityIntent.putExtras(intent);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(activityIntent);
    }

    /**
     * @param context
     * @param pushData
     * @throws JSONException
     * @throws NameNotFoundException
     * @return true: has new version, false: current version is already up to date
     *        
     */
    private boolean checkNewVersion(Context context, JSONObject pushData) throws JSONException, NameNotFoundException {
        int newVerCode = pushData.getInt("version_code");
        PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        int oldVerCode = pInfo.versionCode;
        
        if(oldVerCode < newVerCode){
            return true;
        }
        return false;
    }
}
